<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title">文化馆预约管理系统</div>
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :loading="loading">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
        <div class="register-link">
          <span>还没有账号？</span>
          <router-link to="/register">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    return {
      // 登录表单数据
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      },
      // 是否正在登录
      loading: false
    }
  },
  methods: {
    // 登录方法
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        this.loading = true
        try {
          //发送登录请求
          const { data: res } = await this.$http.post('/user/login', this.loginForm)
        // const res={
        //     code:200,
        //     data:{
        //         token:'123456',
        //         userInfo:{
        //             username:'admin',
        //             role:'ADMIN'
        //         }
        //     }
        // }
          if (res.code !== 200) {
            return this.$message.error(res.message || '登录失败')
          }
          // 登录成功
          this.$message.success('登录成功')
          // 保存token和用户信息
          this.$store.dispatch('login', {
            token: res.data.token,
            userInfo: res.data.userInfo
          })
          // 根据角色跳转到不同页面
          if (res.data.userInfo.role === 'ADMIN') {
            this.$router.push('/admin')
          } else {
            this.$router.push('/home')
          }
        } catch (error) {
          console.error('登录失败', error)
          this.$message.error('登录失败，请稍后重试')
        } finally {
          this.loading = false
        }
      })
    },
    // 重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  padding: 20px;
  box-sizing: border-box;
}

.title {
  font-size: 24px;
  text-align: center;
  margin: 20px 0 30px;
  color: #303133;
  font-weight: bold;
}

.login-form {
  padding: 0 20px;
}

.btns {
  display: flex;
  justify-content: center;
}

.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #606266;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
}
</style>